class_name Camera
extends Camera2D


## 抖动的持续时间（秒）
@export var duration_shake: float = 0.05
## 抖动的强度
@export var shake_intensity: float = 1.0


var is_shaking: bool = false
var time_shaking: float


func _init():
	DamageManager.heavy_blow_receive.connect(on_heavy_blow_received)



func _process(delta: float):
	if is_shaking:
		process_shaking(delta)


func process_shaking(delta: float):
	if time_shaking < 0.0:
		is_shaking = false
		time_shaking = 0.0
		offset = Vector2.ZERO
		return
	time_shaking -= delta
	offset = Vector2(randf_range(-shake_intensity, shake_intensity), randf_range(-shake_intensity, shake_intensity))


func on_heavy_blow_received():
	if not OptionsManager.is_screen_shake_enabled:
		return
	if is_shaking:
		return
	is_shaking = true
	time_shaking = duration_shake
